import record from './record'
import utils from './utils'
import report from './report'

export default _ => {
    let pageLoad = false;
    window.addEventListener('load', function() {
        pageLoad = true;
        console.log('addEventListener onload')
        checkResources()
    })

    function checkResources() {
      //  console.log(request)
        for (let item in request) {
            if (request[item]) {
                if (request[item] == 'loading') {
                    return false;
                }
            }
        }
        if(!pageLoad){
          return false;
        }
        console.log('资源加载完毕',request)
        request = [];
        //浏览器是否支持
        if (!window.performance && !window.performance.getEntries) {
            return false;
        }
        var result = [];
        // 获取当前页面所有请求对应的PerformanceResourceTiming对象进行分析
        window.performance.getEntries().forEach(function (perf) {
            result.push({
                'url': perf.name,
                'entryType': perf.entryType,
                'type': perf.initiatorType,
                'duration(ms)': perf.duration
            });
        });
        report.performance(result);
        // 控制台输出统计结果
        //console.log(result);
    }
    let request = {};
    var XHR = XMLHttpRequest.prototype;
    if (!XHR) return;
    var open = XHR.open;
    // var onreadystatechange = XHR.onreadystatechange;
    var send = XHR.send;
    let type,
        url,
        startTime, endTime;
    XHR.open = function(method, reqUrl) {
        type = method;
        this.url = reqUrl;
       
        open.apply(this, arguments);
    }

    XHR.send = function() {
        var _this = this;
        if (!pageLoad) {
            request[this.url] = "loading";
            console.log('页面没有加载完',JSON.stringify(request))
        }


        startTime = utils.getTime();

        let onreadystatechange = _this.onreadystatechange
        _this.onreadystatechange = function() {
                // console.log('_this.readyState')
                // console.log(_this.readyState)
                // console.log(_this.status)
                if (_this.readyState == 4 && _this.status == 200) {
                    // console.log('接口',_this.responseURL || url)
                    // if (!pageLoad) {
                    //     request[url] = "done";
                    // }
                    // console.log('***************')
                    // console.log(_this.responseURL)
                    // console.log(type)
                    // console.log(this.url)
                    // console.log('***************')
                    if (request[this.url] == 'loading') {
                        request[this.url] = 'done';
                        checkResources();
                    }
                    endTime = utils.getTime();
                    record.performance.push({
                        type: 'API',
                        method: type,
                        startTime: startTime,
                        endTime: endTime,
                        url: _this.responseURL || url
                    })
                }
                // console.log('onreadystatechange')
                onreadystatechange.apply(this, arguments);
            }
            // console.log('send')
        send.apply(this, arguments);
    }

}